package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test5 {
    public static void main(String[] args) throws Exception{
        File dir = new File(Test5.class.getResource(".").toURI());
        File[] files = dir.listFiles(f ->f.getName().contains(".class"));
        for(File file :files){
            String className = file.getName().replace(".class","");
            Class cls = Class.forName(Test5.class.getPackage().getName()+"."+className);
            if(cls.isAnnotationPresent(AutoRunClass.class)){
                Object o = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for(Method method : methods){
                    if(method.isAnnotationPresent(AutoRunMethod.class)){
                        AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
                        int value = arm.value();
                        for (int i = 0; i <value; i++) {
                            method.invoke(o);
                        }
                    }
                }
            }

        }
    }
}
